package breeze.pixel.weather.apps_util.utils;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;

import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;

public class MDialog extends AlertDialog {

    public static String TAG = MDialog.class.getSimpleName();

    @SuppressLint("StaticFieldLeak")
    private static View dialig_view;

    @SuppressLint("StaticFieldLeak")
    private static MDialog m;

    @SuppressLint("StaticFieldLeak")
    private static MTextView title2,content,positive_botton,negative_botton,navigate_button,sub_title;

    @SuppressLint("InflateParams")
    public MDialog(Context context) {
        super(context);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialig_view=LayoutInflater.from(context).inflate(R.layout.app_dialog,null);
        content=dialig_view.findViewById(R.id.dialog_content);
        title2=dialig_view.findViewById(R.id.dialog_title);
        positive_botton=dialig_view.findViewById(R.id.dialog_positive_button);
        negative_botton=dialig_view.findViewById(R.id.dialog_negative_button);
        navigate_button=dialig_view.findViewById(R.id.dialog_navigate_button);
        sub_title = dialig_view.findViewById(R.id.dialog_subtitle);
    }

    @Override
    public void setTitle(CharSequence title) {
        title2.setVisibility(View.VISIBLE);
        title2.setText(title);
    }

    public void setSubTitle(CharSequence subTitle){
        sub_title.setVisibility(View.VISIBLE);
        sub_title.setText(subTitle);
    }

    public void setTitleTextColor(int color) {
        title2.setTextColor(color);
    }

    public void setTitleTextSize(int size) {
        title2.setTextSize(size);
    }

    public void setSubTitleTextColor(int color) {
        sub_title.setTextColor(color);
    }

    public void setSubTitleTextSize(int size) {
        sub_title.setTextSize(size);
    }

    @Override
    public void setMessage(CharSequence message) {
        content.setVisibility(View.VISIBLE);
        content.setText(message);
    }

    public void setContentTextColor(int color) {
        content.setTextColor(color);
    }

    public void setContentTextSize(int size) {
        content.setTextSize(size);
    }

    @Override
    public void show() {
        try {
            this.setView(dialig_view,0,0,0,0);
            super.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setPositiveButton(String text, final onClick p) {
        positive_botton.setVisibility(View.VISIBLE);
        positive_botton.setText(text);
        positive_botton.setOnClickListener(new MTextView.OnClickListener(){
                @Override
                public void onClick(View p1) {
                    if (p==null) {
                        MDialog.this.dismiss();
                    } else {
                        p.onclick();
                        MDialog.this.dismiss();
                    }
                }
            });
    }

    public void setNegativeBotton(String text, final onClick p) {
        negative_botton.setVisibility(View.VISIBLE);
        negative_botton.setText(text);
        negative_botton.setOnClickListener(new MTextView.OnClickListener(){
                @Override
                public void onClick(View p1) {
                    if (p==null) {
                        MDialog.this.dismiss();
                    } else {
                        p.onclick();
                        MDialog.this.dismiss();
                    }
                }
            });
    }

    public void setNavigateBotton(String text, final onClick p) {
        navigate_button.setVisibility(View.VISIBLE);
        navigate_button.setText(text);
        navigate_button.setOnClickListener(new MTextView.OnClickListener(){
                @Override
                public void onClick(View p1) {
                    if (p==null) {
                        MDialog.this.dismiss();
                    } else {
                        p.onclick();
                        MDialog.this.dismiss();
                    }
                }
            });
    }

    public void setPositiveColor(int color) {
        positive_botton.setTextColor(color);
    }

    public void setNegativeColor(int color) {
        negative_botton.setTextColor(color);
    }

    public void setNavigateColor(int color) {
        navigate_button.setTextColor(color);
    }
	
	public void setPositiveTextSize(int size) {
        positive_botton.setTextSize(size);
    }

    public void setNegativeTextSize(int size) {
        negative_botton.setTextSize(size);
    }

    public void setNavigateTextSize(int size) {
        navigate_button.setTextSize(size);
    }

    public interface onClick {
        void onclick();
    }

    public static void show(final Context context, final String title, final String content) {
        cancelDialog();
        m=new MDialog(context);
        if (title!=null) {
            m.setTitle(title);
        }
        m.setTitleTextSize(15);
        m.setContentTextSize(13);
        m.setMessage(content);
        m.setPositiveButton("确定",null);
        m.show();
    }

    public static void cancelDialog() {
        if (m!=null) {
            m.cancel();
        }
    }

}
